package com.tjbklx1.thread.chap02.practise06;

/**
 * 可变类
 * 可变与不可变的相互转化
 * @author Administrator
 *
 */
public class MutablePerson {

	private String name;
	private String address;

	public MutablePerson(String name, String address) {
		super();
		this.name = name;
		this.address = address;
	}

	public String getName() {
		return name;
	}

	public String getAddress() {
		return address;
	}
	
	//可变类的同步方法 防止出现非同步的问题
	@Override
	public synchronized String toString() {
		return "[ name = " + name + ",address = " + address + "]";
	}

	// 可变类提供传入不可变的对象进行封装 讲不可变对象转成可变对象
	public MutablePerson(ImmutablePerson person) {
		this.name = person.getName();
		this.address = person.getAddress();
	}
	
	public synchronized void setPerson(String name, String address){
		this.name = name;
		this.address = address;
	}
	// 获取可变对象的不可变形式
	public synchronized ImmutablePerson getImmutablePerson() {
		return new ImmutablePerson(this);
	}

}
